/**
 * 
 */
package cn.com.winning.ecare.common;

/**
 * @author ZJJ
 */
import java.io.File;

import android.util.Log;

public class DeleteDirectory
{

	boolean flag = true;
	File file = null;
	private static DeleteDirectory directory = null;
	
	private DeleteDirectory(){
		
	}
	
	public static DeleteDirectory getDeleteDirectory(){
		if(directory==null){
			directory = new DeleteDirectory();
		}
		return directory;
	}

	/**
	 * 删除空目录
	 * 
	 * @param dir
	 *        将要删除的目录路径
	 */
	public static void doDeleteEmptyDir(String dir)
	{
		boolean success = (new File(dir)).delete();
		if (success)
		{
			System.out.println("Successfully deleted empty directory: " + dir);
		}
		else
		{
			System.out.println("Failed to delete empty directory: " + dir);
		}
	}

	/**
	 * 递归删除目录下的所有文件及子目录下所有文件
	 * 
	 * @param dir
	 *        将要删除的文件目录
	 * @return boolean Returns "true" if all deletions were successful. If a
	 *         deletion fails, the method stops attempting to delete and returns
	 *         "false".
	 */
	public static boolean deleteDir(File dir)
	{
		if (dir.isDirectory())
		{
			String[] children = dir.list();
			// 递归删除目录中的子目录下
			for (int i = 0; i < children.length; i++)
			{
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success)
				{
					return false;
				}
			}
		}
		// 目录此时为空，可以删除
		return dir.delete();
	}

	// 验证字符串是否为正确路径名的正则表达式
	private static String matches = "[A-Za-z]:\\\\[^:?\"><*]*";

	// 通过 sPath.matches(matches) 方法的返回值判断是否正确
	// sPath 为路径字符串

	/**
	 * 根据路径删除指定的目录或文件，无论存在与否
	 * 
	 * @param sPath
	 *        要删除的目录或文件
	 * @return 删除成功返回 true，否则返回 false。
	 */
	public boolean DeleteFolder(String sPath)
	{
		flag = false;
		file = new File(sPath);
		// 判断目录或文件是否存在
		if (!file.exists())
		{ // 不存在返回 false
			return flag;
		}
		else
		{
			// 判断是否为文件
			if (file.isFile())
			{ // 为文件时调用删除文件方法
				return deleteFile(sPath);
			}
			else
			{ // 为目录时调用删除目录方法
				return deleteDirectory(sPath);
			}
		}
	}

	/**
	 * 删除单个文件
	 * 
	 * @param sPath
	 *        被删除文件的文件名
	 * @return 单个文件删除成功返回true，否则返回false
	 */
	public boolean deleteFile(String sPath)
	{
		flag = false;
		file = new File(sPath);
		// 路径为文件且不为空则进行删除
		if (file.isFile() && file.exists())
		{
			file.delete();
			flag = true;
		}
		return flag;
	}

	/**
	 * 删除目录（文件夹）以及目录下的文件
	 * 
	 * @param sPath
	 *        被删除目录的文件路径
	 * @return 目录删除成功返回true，否则返回false
	 */
	public boolean deleteDirectory(String sPath)
	{
		// 如果sPath不以文件分隔符结尾，自动添加文件分隔符
		if (!sPath.endsWith(File.separator))
		{
			sPath = sPath + File.separator;
		}
		File dirFile = new File(sPath);
		// 如果dir对应的文件不存在，或者不是一个目录，则退出
		if (!dirFile.exists() || !dirFile.isDirectory())
		{
			return false;
		}
		flag = true;
		// 删除文件夹下的所有文件(包括子目录)
		File[] files = dirFile.listFiles();
		for (int i = 0; i < files.length; i++)
		{
			// 删除子文件
			if (files[i].isFile())
			{
				flag = deleteFile(files[i].getAbsolutePath());
				if (!flag)
					break;
			} // 删除子目录
			else
			{
				flag = deleteDirectory(files[i].getAbsolutePath());
				if (!flag)
					break;
			}
		}
		if (!flag)
			return false;
		// 删除当前目录
		if (dirFile.delete())
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public void deleteSDCardFolder(File dir)
	{
		File to = new File(dir.getAbsolutePath() + System.currentTimeMillis());
		dir.renameTo(to);
		if (to.isDirectory())
		{
			String[] children = to.list();
			for (int i = 0; i < children.length; i++)
			{
				File temp = new File(to, children[i]);
				if (temp.isDirectory())
				{
					deleteSDCardFolder(temp);
				}
				else
				{
					boolean b = temp.delete();
					if (b == false)
					{
						Log.d("deleteSDCardFolder", "DELETE FAIL");
					}
				}
			}
			to.delete();
		}
	}


	/**
	 * 测试
	 */
	public static void main(String[] args)
	{
		doDeleteEmptyDir("new_dir1");
		String newDir2 = "new_dir2";
		boolean success = deleteDir(new File(newDir2));
		if (success)
		{
			System.out.println("Successfully deleted populated directory: " + newDir2);
		}
		else
		{
			System.out.println("Failed to delete populated directory: " + newDir2);
		}
	}
}
